package com.itheima.stringdemo;

import java.util.Scanner;

public class StringDemo9 {
    public static void main(String[] args) {
        //需求：键盘录入一个小于或等于7位数的整数，转成大写的形式
        //例如：2135变成 零佰零拾零万贰仟壹佰零拾伍元

        //1.键盘录入一个整数
        Scanner sc = new Scanner(System.in);
        int number;

        //对number进行合理性的判断
        while (true) {
            System.out.println("请输入金额：");
            number = sc.nextInt();
            if (number >= 0 && number <= 9999999) {
                break;
            } else {
                System.out.println("您输入的金额非法，请重新输入：");
            }
        }

        //定义变量用来表示钱的大写
        String str1 = "";

        //2.得到number里面的每一个数字
        while (number != 0) {
            String CapitalNumber = getCapitalnumber(number % 10);
            str1 = CapitalNumber + str1;
            number /= 10;
        }

        //3.在前面补0，补齐七位
        int count = 7 - str1.length();
        for (int i = 0; i < count; i++) {
            str1 = "零" + str1;
        }

        //4.插入单位
        //定义一个数组表示单位
        String[] arr2 = {"佰", "拾", "万", "仟", "佰", "拾", "元"};
        //定义一个字符串将金额和单位联系起来
        String result = "";
        for (int i = 0; i < str1.length(); i++) {
            //拼接字符
            result = result + str1.charAt(i) + arr2[i];
        }

        //5.打印最终结果
        System.out.println(result);
    }

    //定义一个方法用来改变大写格式
    public static String getCapitalnumber(int number) {
        //定义字符串数组存储大写的格式
        String[] arr1 = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        return arr1[number];
    }
}
